package org.ielse.frame;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;

import org.ielse.frame.BaseActivity.VolleyListener;
import org.ielse.frame.util.BaseUtil;

import java.util.Map;
import java.util.Set;

public class BaseFragment extends Fragment {

	protected BaseActivity activity;

	@Override public void onAttach(Activity activity) {
		super.onAttach(activity);
		if (activity instanceof BaseActivity) {
			this.activity = (BaseActivity) activity;
		}
	}

	@Override public void onDetach() {
		super.onDetach();
		this.activity = null;
	}

	@Override public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override public void onDestroy() {
		super.onDestroy();
	}

	public void showToast(int resId) {
		if (activity != null) activity.showToast(resId);
	}

	public void showToast(String text) {
		if (activity != null) activity.showToast(text);
	}

	public void startActivity(Class<?> target, Object... params) {
		if (activity != null) activity.startActivity(target, BaseUtil.initBundle(params));
	}

	public void startActivity(Class<?> target, Bundle bundle) {
		if (activity != null) activity.startActivity(target, bundle);
	}

	public void logi(String text) {
		if (activity != null) activity.logi(text);
	}

	public void logd(String text) {
		if (activity != null) activity.logd(text);
	}

	public void logw(String text) {
		if (activity != null) activity.logw(text);
	}

	public void loge(String text) {
		if (activity != null) activity.loge(text);
	}

	public void writeToSpfs(String key, Object value) {
		if (activity != null) activity.writeToSpfs(key, value);
	}

	public String readStringFromSpfs(String key) {
		return activity != null ? activity.readStringFromSpfs(key) : null;
	}

	public String readStringFromSpfs(String key, String defValue) {
		return activity != null ? activity.readStringFromSpfs(key, defValue) : null;
	}

	public int readIntFromSpfs(String key) {
		return activity != null ? activity.readIntFromSpfs(key) : -1;
	}

	public int readIntFromSpfs(String key, int defValue) {
		return activity != null ? activity.readIntFromSpfs(key, defValue) : -1;
	}

	public float readFloatFromSpfs(String key) {
		return activity != null ? activity.readFloatFromSpfs(key) : -1f;
	}

	public float readFloatFromSpfs(String key, float defValue) {
		return activity != null ? activity.readFloatFromSpfs(key, defValue) : -1f;
	}

	public boolean readBooleanFromSpfs(String key) {
		return activity != null ? activity.readBooleanFromSpfs(key) : false;
	}

	public boolean readBooleanFromSpfs(String key, boolean defValue) {
		return activity != null ? activity.readBooleanFromSpfs(key, defValue) : false;
	}

	public long readLongFromSpfs(String key) {
		return activity != null ? activity.readLongFromSpfs(key) : -1l;
	}

	public long readLongFromSpfs(String key, long defValue) {
		return activity != null ? activity.readLongFromSpfs(key, defValue) : -1l;
	}

	public Set<String> readStringSetFromSpfs(String key) {
		return activity != null ? activity.readStringSetFromSpfs(key) : null;
	}

	public Set<String> readStringSetFromSpfs(String key, Set<String> defValues) {
		return activity != null ? activity.readStringSetFromSpfs(key, defValues) : null;
	}

	public boolean containsFromSpfs(String key) {
		return activity != null ? activity.containsFromSpfs(key) : false;
	}

	public boolean removeSpfs(String key) {
		return activity != null ? activity.removeSpfs(key) : false;
	}

	public boolean clearSpfs() {
		return activity != null ? activity.clearSpfs() : false;
	}

	public <T> void post(final String url, final Map<String, String> params, final Class<T> clazz, final VolleyListener<T> listener) {
		if (activity != null) activity.post(url, params, clazz, listener);
	}

	public <T> void get(final String url, final Map<String, String> params, final Class<T> clazz, final VolleyListener<T> listener) {
		if (activity != null) activity.get(url, params, clazz, listener);
	}

	public View inflater(int resource) {
		return activity != null ? activity.inflater(resource) : null;
	}

	public void showDialog(View content, boolean canCancel, boolean shadow) {

	}
}
